﻿using Awsc.DataAcquisitionSys.Core.Model.Config;
using Awsc.DataAcquisitionSys.Core.Models.ServiceModels;
using Awsc.DataAcquisitionSys.Core.Services.DataAcquisition.DataCollectors;

namespace Awsc.DataAcquisitionSys.Core.Service.DataAcquisition.DataCollectors
{
    public class AIOBOXCollector : ModbusTcpDataCollector
    {        //aiobox采集器过程数据定义
        const float MinValue = 0;//采集器额定范围读取AI最小值 0-300做为采集器误差忽略
        const float MaxValue = 27649;//采集器额定范围读取AI最大值
        const float CurrentMin = 4;//采集器额定范围电流最小值 
        const float CurrentMax = 20;//采集器额定范围电流最大值
        public AIOBOXCollector(DataCollectionContext dataCollectionContext) : base(dataCollectionContext)
        {
        }

        protected override async Task CollectDataInternalAsync()
        {
            //读取数字量DI
            if (CollectorConfig.DICount > 0)
            {
                //读取数字量DI
                bool[] ds = ModbusMaster.ReadInputs(CollectorConfig.SlaveAddress, 0, CollectorConfig.DICount);

                for (int i = 0; i < ds.Length && i < CollectorConfig.DICount; i++)
                {
                    SetDICoilValue(i + 1, ds[i]);
                }
                ////读取输入计数值
                //ushort[] cs = master.ReadInputRegisters(Collector.SlaveAddress, 0, (ushort)(Collector.DICount * 2));
                //int j = 0;
                //for (int i = 0; i < cs.Length && j < counterValues_DI.Length; i += 2, j++)
                //{
                //    counterValues_DI[j] = (uint)((cs[i] << 16) | (cs[i + 1]));
                //}
            }

            //读取数字量DO
            if (CollectorConfig.DOCount > 0)
            {
                bool[] points = ModbusMaster.ReadCoils(CollectorConfig.SlaveAddress, CollectorConfig.DICount, CollectorConfig.DOCount);
                for (int i = 0; i < points.Length && i < CollectorConfig.DOCount; i++)
                {
                    SetDOCoilValue(i + 1, points[i]);
                }
            }

            // 采集器输出的数据量，非传感器电流电压值
            if (CollectorConfig.AICount > 0)
            {
                ushort[] ushortArray = ModbusMaster.ReadInputRegisters(CollectorConfig.SlaveAddress, (ushort)(CollectorConfig.DICount * 2), CollectorConfig.AICount);
                short[] AIValue = new short[ushortArray.Length];

                for (int i = 0; i < AIValue.Length; i++)
                {
                    AIValue[i] = unchecked((short)ushortArray[i]);
                }
                if (AIValue != null)
                {
                    for (int i = 0; i < AIValue.Length; i++)
                    {

                        var value = CurrentMin;
                        var status = DevStatus.S_OK;

                        if (AIValue[i] == -0x8000)
                        {
                            //value = CurrentMin;
                            status = DevStatus.S_UNKNOWN;
                        }
                        else if (AIValue[i] < MinValue)
                        {
                            //value = CurrentMin;
                            //status = DevStatus.S_OK;

                        }
                        else if (AIValue[i] > MaxValue)
                        {
                            value = CurrentMax;
                            //status = DevStatus.S_OK;
                        }
                        else
                        {
                            value = CurrentMin + AIValue[i] * (CurrentMax - CurrentMin) / (MaxValue - MinValue);//换算公式,返回真实电流值 
                                                                                                                //status = DevStatus.S_OK;
                        }
                        SetAISensorValue(i + 1, value, status);
                    }
                }
            }
        }

    }
}
